﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using ch.froorider.NGoogleCalendar.googleService;
using ch.froorider.NGoogleCalendar.tools;

namespace GoogleCalendarView
{
	/// <summary>
	/// Interaction logic for Window1.xaml
	/// </summary>
	public partial class MainView : Window
	{

		public MainView()
		{
			this.InitializeComponent();
			OnInit(this, null);	
		}

		/// <summary>
		/// Constructor. Inits the Gui
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void OnInit(object sender, EventArgs e)
		{
			
		}

		#region Functionality (public methods)

		/// <summary>
		/// Saves a new user to the DB.
		/// </summary>
		/// <param name="accountName"></param>
		/// <param name="userName"></param>
		/// <param name="password"></param>
		public void SaveNewUser(string accountName, string userName, string password)
		{
			try
			{
				this.loggingBlock.Text = "User added with ID: ";
			}
			catch (Exception exc)
			{
				MessageBox.Show("Exception occured: " + exc.Message + " " + exc.StackTrace);
			}
		}

		#endregion

		#region Command handlers

		// Executed event handler.
		private void CloseCommandHandler(object sender, ExecutedRoutedEventArgs e)
		{
			Application.Current.Shutdown();
		}

		// CanExecute event handler.
		private void CanExecuteHandler(object sender, CanExecuteRoutedEventArgs e)
		{
			e.CanExecute = true;
		}

		/// <summary>
		/// Logic which "askes" for the parameter for a new user account.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void NewUserCommandHandler(object sender, ExecutedRoutedEventArgs e)
		{
			this.loggingBlock.Text = "Creating new user";
			UserEntryPanel userPanel = new UserEntryPanel();
			userPanel.Owner = this;
		}

		/// <summary>
		/// Deletes an existing user out of the DB.
		/// TODO: At the moment the user with the biggest index is deleted not a specific one.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void DeleteUserCommandHandler(object sender, ExecutedRoutedEventArgs e)
		{
			try
			{
				
			}
			catch (Exception exc)
			{
				MessageBox.Show("Exception occured: " + exc.Message + " " + exc.StackTrace);
			}
		}

		#endregion

		#region EventHandler
		
		/// <summary>
		/// Shows the calendars according to the selection. Is automatically called, when the Index is selt in XAML.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void topBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			calendarListBox.Items.Clear();
			int selectedIndex = topBox.SelectedIndex;

			
		}

		#endregion

		#region Google API Thread

		public static void SynchronizeWithGoogle(object args)
		{
		
			while(true)
			{
			
			}
		}

		#endregion
	}
}
